package com.example.task01_demo01;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //6.设置请求信息中的编码方式为utf-8来解决乱码问题
        request.setCharacterEncoding("utf-8");

        //1.获取指定参数名称对应的参数值并打印
        String name = request.getParameter("name");
        System.out.println("获取到的姓名为:"+name);

        String[] hobbies = request.getParameterValues("hobby");
        System.out.print("获取到的爱好为:");
        for(String ts:hobbies){
            System.out.print(ts+" ");
        }
        System.out.println();
        System.out.println("__________________________________________________________");

        //2.获取所有参数的名称
        Enumeration<String> parameterNames = request.getParameterNames();
        System.out.print("获取到的所有参数名称为:");
        while(parameterNames.hasMoreElements()){
            System.out.print(parameterNames.nextElement()+" ");
        }
        System.out.println();
        System.out.println("__________________________________________________________");

        //3.获取请求参数名和对应值的第二种方式
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将Map集合中所有的键值对组成Set集合
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        //遍历Set集合
        for(Map.Entry<String, String[]> me:entries){
            System.out.print(me.getKey()+"对应的数值有:");
            for(String ts:me.getValue()){
                System.out.print(ts+" ");
            }
            System.out.println();
        }

        System.out.println("__________________________________________________________");



        //4.获取客户端请求的其他信息
        System.out.println("发送请求的客户端IP地址为:"+request.getRemoteAddr());
        System.out.println("发送请求的客户端端口号为:"+request.getRemotePort());
        System.out.println("请求资源的路径为:"+request.getRequestURI());
        System.out.println("请求资源的完整路径为:"+request.getRequestURL());
        System.out.println("请求方式为:"+request.getMethod());
        System.out.println("请求的附带参数为:"+request.getQueryString());
        System.out.println("请求的Servlet的路径为:"+request.getServletPath());

        System.out.println("__________________________________________________________");


        //获取响应数据的默认编码方式
        String characterEncoding=response.getCharacterEncoding();
        System.out.println("服务器响应数据的默认编码方式为:"+characterEncoding);//ISO-8859-1

        //设置服务器和浏览器的编码方式以及文本类型
        response.setContentType("text/html;charset=UTF-8");

        //5.向浏览器发出响应数据
        PrintWriter writer=response.getWriter();
        //发字符串
        //writer.write("I Received!");
        //writer.write("我接收到了!");

        //发随机数
        //Random ra=new Random();
        //int num = ra.nextInt(100) + 1;
        //writer.write("<h1>"+num+"</h1>");

        //发时间
        Date d1=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat();
        String format = sdf.format(d1);
        writer.write("当前系统时间为:"+format);
        System.out.println("服务器发送数据成功!");
        writer.close();
    }
}
